"""
Choice module

Copyright (c) 2009 John Markus Bjoerndalen <jmb@cs.uit.no>,
      Brian Vinter <vinter@diku.dk>, Rune M. Friborg <runef@diku.dk>.
See LICENSE.txt for licensing details (MIT License). 
"""

from constants import *

# Decorators
def choice(func):
    """
    Decorator for creating choice objets
    
    >>> @choice
    ... def action(__channel_input):
    ...     print 'Hello'

    >>> from guard import Skip
    >>> Alternation([{Skip():action}]).execute()
    Hello
    """
    # __choice_fn func_name used to identify function in Alternation.execute
    def __choice_fn(*args, **kwargs):
        return Choice(func, *args, **kwargs)
    return __choice_fn


# Classes
class Choice:
    """ Choice(func, *args, **kwargs)
    It is recommended to use the @choice decorator, to create Choice instances
    """
    def __init__(self, fn, *args, **kwargs):
        self.fn = fn
        self.args = args
        self.kwargs = kwargs

    def invoke_input_action(self, __channel_input):
        self.kwargs['__channel_input'] = __channel_input
        self.fn(*self.args, **self.kwargs)

    def invoke_output_action(self):
        self.fn(*self.args, **self.kwargs)


